
local FoldersToSearch = nil

/*---------------------------------------------------------
   Name: Make the table if it doesn't exist
   Note: This should never happen, since I'll be shipping a database with it.. but just in case..
---------------------------------------------------------*/
if ( !sql.TableExists( "searchcache" ) ) then

	sql.Query( "CREATE TABLE IF NOT EXISTS searchcache ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, modelname TEXT );" )
	
end

/*---------------------------------------------------------
   Name: Builds the search index for this specific folder
---------------------------------------------------------*/
local function BuildSearchIndex( strFolder )

	// Add Models
	local files = file.Find( "../" .. strFolder .. "*.mdl" )
	for k, v in pairs( files ) do
	
		// Filter out unusable crap
		if ( !UTIL_IsUselessModel( v ) ) then
		
			local ModelName = SQLStr( strFolder..v )
			sql.Query( "INSERT INTO searchcache ( modelname ) VALUES ( "..ModelName.." )" )
			
		end
	
	end
	
	// Add folders in this folder that need searching
	local files = file.FindDir( "../" .. strFolder .. "*" )
	for k, v in pairs( files ) do
	
		local SubFolder = Format( "%s%s/", strFolder, v  )	
		table.insert( FoldersToSearch, SubFolder )
	
	end
	
	

end

/*---------------------------------------------------------
   SearchIndexBuildThink 
	Called every think to continue building the cache
	(To prevent it freezing the game while it's building)
---------------------------------------------------------*/
local function SearchIndexBuildThink()

	// If there aren't any folders left to search, then don't
	if ( !FoldersToSearch ) then return end
	
	// Process one folder (break prevents it from doing all)
	for k, v in pairs( FoldersToSearch ) do
	
		BuildSearchIndex( v )
		FoldersToSearch[ k ] = nil
		break
	
	end
	
	// We have just processed the last folder, tell the user
	if ( #FoldersToSearch == 0 ) then
	
		local ModelCount = sql.QueryValue( "SELECT count(*) FROM searchcache" )
		LocalPlayer():PrintMessage( HUD_PRINTTALK, "Search Cache Built! Found "..tostring(ModelCount).." models." );
		FoldersToSearch = nil
		
		sql.Commit()
		
	end

end

hook.Add( "Think", "SearchIndexBuildThink", SearchIndexBuildThink )

/*---------------------------------------------------------
   RebuildSearchCache 
	Wipes the old data and completely replaces it
---------------------------------------------------------*/
function RebuildSearchCache()

	// A build is in progress!
	if ( FoldersToSearch != nil ) then return end

	// Add topmost folder to search
	FoldersToSearch = { "models/" }
	
	// Remove all from the searchcache table
	sql.Query( "DELETE FROM searchcache" )
	
	
	LocalPlayer():PrintMessage( HUD_PRINTTALK, "Building the search cache (This can take up to a minute).." );
	
	sql.Begin()
	
end

/*---------------------------------------------------------
   ModelSearch 
---------------------------------------------------------*/
function ModelSearch( SearchString, iLimit )

	iLimit = iLimit or 128

	SearchString = SQLStr( SearchString, true )
	
	local result = sql.Query( "SELECT modelname FROM searchcache WHERE modelname LIKE '%"..SearchString.."%' LIMIT "..iLimit )
	if (!result) then return {} end
	
	local ret = {}
	
	for k, v in pairs( result ) do
	
		table.insert( ret, v.modelname )
	
	end
	
	table.sort( ret )
	
	return ret
	
end
